/*
    Invisible Hand - Minecraft Plugin for buying and selling commodities
    Copyright (C) 2012 Ernest Pazera

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
package com.pdg.mcplugin.ih;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class InvisibleHand  extends JavaPlugin{
	
	//logger
	private Logger logger;
	public Logger getLogger(){
		if(logger==null){
			logger = Logger.getLogger(Constants.loggerName);
		}
		return logger;
	}
	
	//plugin manager
	private PluginManager pluginManager;
	public PluginManager getPluginManager(){
		if(pluginManager == null){
			pluginManager = getServer().getPluginManager();
		}
		return pluginManager;
	}
	
	private IHServerListener serverListener = null;
	public IHServerListener getServerListener(){
		if(serverListener==null){
			serverListener = new IHServerListener(this);
		}
		return serverListener;
	}
	
	public void onEnable(){
		getLogger().info(Constants.messagePluginEnabled);
		getPluginManager().registerEvent(Type.PLUGIN_ENABLE, getServerListener(), Priority.Monitor, this);
		getPluginManager().registerEvent(Type.PLUGIN_DISABLE, getServerListener(), Priority.Monitor, this);
	}

	public void onDisable(){
		getLogger().info(Constants.messagePluginDisabled);
	}
	
	private void showHelp(CommandSender theCommandSender,String[] theLines){
		for(String line:theLines){
			theCommandSender.sendMessage(line);
		}
	}
	
	private void showGeneralHelp(CommandSender theCommandSender){
		showHelp(theCommandSender,Constants.helpGeneral);
	}
	
	private void showBadCommand(CommandSender theCommandSender){
		theCommandSender.sendMessage(Constants.errorBadCommand);
	}
	
	private void handleIHQuestionSubcommand(CommandSender theCommandSender, List<String> theArgs){
		showGeneralHelp(theCommandSender);
	}
	
	private void handleIHCommand(CommandSender theCommandSender, List<String> theArgs){
		if(theArgs.size()==Constants.argCountZero){
			showGeneralHelp(theCommandSender);
			return;
		}else{
			String theSubcommand = theArgs.get(Constants.argCountZero);
			theArgs.remove(Constants.argCountZero);
			if(theSubcommand.equalsIgnoreCase(Constants.subcommandHelp) || theSubcommand.equalsIgnoreCase(Constants.subcommandQuestion)){
				handleIHQuestionSubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandAdmin)){
				handleIHAdminSubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandBuy)){
				handleIHBuySubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandPrice)){
				handleIHPriceSubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandPrices)){
				handleIHPricesSubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandSell)){
				handleIHSellSubcommand(theCommandSender,theArgs);
			}else if(theSubcommand.equalsIgnoreCase(Constants.subcommandTradingpost)){
				handleIHTradingPostSubcommand(theCommandSender,theArgs);
			}else{
				showBadCommand(theCommandSender);
			}
		}
	}
	
	
	private void handleIHTradingPostSubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showTradingPostHelp(theCommandSender);
			return;
		}else{
			showBadCommand(theCommandSender);
			return;
		}
	}

	private void showTradingPostHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpTradingpost);
	}

	private void handleIHSellSubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showSellHelp(theCommandSender);
			return;
		}else{
			showBadCommand(theCommandSender);
			return;
		}
	}

	private void showSellHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpSell);
	}

	private void handleIHPricesSubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showPricesHelp(theCommandSender);
			showPricesPage(theCommandSender,1);
			return;
		}else{
			Integer thePage = Integer.parseInt(theArgs.get(Constants.argCountZero));
			showPricesPage(theCommandSender,thePage);
			return;
		}
	}

	private void showPricesPage(CommandSender theCommandSender, Integer i) {
		// TODO Auto-generated method stub
		
	}

	private void showPricesHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpPrices);
	}

	private void handleIHPriceSubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showPriceHelp(theCommandSender);
			return;
		}else{
			showBadCommand(theCommandSender);
			return;
		}
	}

	private void showPriceHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpPrice);
	}

	private void handleIHBuySubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showBuyHelp(theCommandSender);
			return;
		}else{
			showBadCommand(theCommandSender);
			return;
		}
	}

	private void showBuyHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpBuy);
	}

	private void handleIHAdminSubcommand(CommandSender theCommandSender,
			List<String> theArgs) {
		if(theArgs.size()==Constants.argCountZero){
			showAdminHelp(theCommandSender);
			return;
		}else{
			showBadCommand(theCommandSender);
			return;
		}
	}

	private void showAdminHelp(CommandSender theCommandSender) {
		showHelp(theCommandSender,Constants.helpAdmin);
	}

	public boolean onCommand(CommandSender theCommandSender, Command theCommand, String theLabel, String[] theArgs){
		if(theLabel.equalsIgnoreCase(Constants.commandIH)){
			handleIHCommand(theCommandSender,Arrays.asList(theArgs));
			return true;
		}else{
			return false;
		}
	}
}
